package com.taxer.controller;

import com.taxer.contants.Constants;
import com.taxer.db.entity.Admin;
import com.taxer.db.entity.Taxer;
import com.taxer.exception.TaxerExcepiton;
import com.taxer.result.Result;
import com.taxer.service.AdminService;
import com.taxer.service.TaxerService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/taxer")
public class TaxerController {

    @Resource
    private TaxerService taxerService;
    @Resource
    private AdminService adminService;
    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(@RequestBody Taxer taxer){
        Result result = new Result();

        try {
           taxerService.insert(taxer);
            result.setMessage("添加成功");
            result.setData(1);


        }catch (TaxerExcepiton taxerExcepiton){
            result.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            result.setMessage(taxerExcepiton.getMessage());
        }
        return result;
    }
    @RequestMapping("/findAll")
    @ResponseBody
    public Result findAll(){
        Result result = new Result();
        try {
            result.setData(taxerService.getAllTaxer());
            result.setMessage("查询成功");

        }catch (TaxerExcepiton taxerExcepiton){
            result.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            result.setMessage(taxerExcepiton.getMessage());
        }
        return result;
    }
    @RequestMapping("/findByName")
    @ResponseBody
    public Result findByName(@RequestBody Taxer taxer){
        Result result = new Result();
        try {
            String name=taxer.getName();
            result.setData(taxerService.getByName(name));
            result.setMessage("查询成功");

        }catch (TaxerExcepiton taxerExcepiton){
            result.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            result.setMessage(taxerExcepiton.getMessage());
        }
        return result;
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Result deleteById(@RequestBody Taxer taxer){
        Result result = new Result();
        try {
            taxerService.deleById(taxer.getId());
            result.setData(1);
            result.setMessage("删除成功");

        }catch (TaxerExcepiton taxerExcepiton){
            result.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            result.setMessage(taxerExcepiton.getMessage());
        }
        return result;
    }
    @RequestMapping("/update")
    @ResponseBody
    public Result update(@RequestBody Taxer taxer){
        Result result = new Result();
        try {
            taxer.setUpdated(new Date());
           taxerService.update(taxer);
            result.setData(1);
            result.setMessage("修改成功");

        }catch (TaxerExcepiton taxerExcepiton){
            result.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            result.setMessage(taxerExcepiton.getMessage());
        }
        return result;
    }
}
